<?php
/**
*
* http://framework.zend.com/wiki/pages/viewpage.action?pageId=10158667
*
*/



class Sgit_Validate_DateLessThan extends Zend_Validate_Abstract
{
 
    const NOT_LESS = 'notLessThan';

 
    protected $_messageTemplates = array(
        self::NOT_LESS => "'%value%' is not less than '%max%'",

    );
 
    protected $_messageVariables = array(
        'max' => '_max'
    );
 
    protected $_max;
 
    protected $_inclusive;
     
    protected $_datePart;
 
    public function __construct( $max, $datePart = Zend_Date::TIMESTAMP )
    {
        
         if( $max instanceof Zend_Date ){
             $this->setMax( $max )->setDatePart( $datePart );
        }else{
             $max = Zend_Controller_Front::getInstance()->getRequest()->getParam($max);
             if(!empty($max) ){
                $max=new Zend_Date($max,Zend_Date::ISO_8601);
                $this->setMax( $max )->setDatePart( $datePart );
            }
        }

    }
 
    public function getMax()
    {
        return $this->_max;
    }
 
    public function setMax( Zend_Date $max )
    {
        $this->_max = $max;
  
        return $this;
    }
 
    public function getDatePart()
    {
        return $this->_datePart;
    }
 
    public function setDatePart( $datePart )
    {
        $this->_datePart = $datePart;
        return $this;
    }
 
    public function isValid( $value )
    {
        if( !$value instanceof Zend_Date )
        {
            try
            {
                $value = new Zend_Date( $value ,Zend_Date::ISO_8601);
            }
            catch( Exception $exception )
            {
                require_once 'Zend/Validate/Exception.php';
                throw new Zend_Validate_Exception( $exception->getMessage() );
            }
        }
 
        $this->_setValue( $value );
 

        
        $validator = new Zend_Validate_Date();
        
        if(!$validator->isValid($this->_max)){
            $this->_error( self::NOT_LESS );
            return false;
        }
        
 
        if( $this->_max->isEarlier( $value, $this->_datePart ) )
        {
            $this->_error( self::NOT_LESS );
            return false;
        }
 
        return true;
    }
 
}